{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from analysis.pre_process import DataProcess\n",
    "\n",
    "# 从指定路径下读取数据集\n",
    "# process = DataProcess(r'C:\\Users\\admin\\Desktop\\mydata')\n",
    "\n",
    "# 默认读取内置的数据集\n",
    "process = DataProcess()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "长期借款(元), 应付债券(元), 以公允价值计量且其变动计入当期损益的金融资产(元), 债权投资(元), 持有至到期投资(元), 其他非流动金融资产(元), 以上字段未找到，已自动赋值为0。\n",
      "\n",
      "！以下字段须手动赋值：其他流动资产里的理财产品,其他流动资产里的结构性存款,长期应付款,应收款项融资,合同负债(元),合同资产(元)\n"
     ]
    }
   ],
   "source": [
    "debt_df = process.read_data('debt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 18年及之前的科目\n",
    "debt_df['其他流动资产里的理财产品'] = [0, 1500000000, 2570000000, 0, 0, 0]\n",
    "debt_df['其他流动资产里的结构性存款'] = [0, 0, 0, 0, 0, 0]\n",
    "\n",
    "# 用来计算有息负债，如果是无息则直接写 0 即可\n",
    "debt_df['长期应付款'] = [0, 0, 0, 0, 0, 0]\n",
    "\n",
    "# 2019 年及之后的科目\n",
    "debt_df['应收款项融资'] = [0, 0, 0, 408972104.07, 0, 0]\n",
    "debt_df['合同资产(元)'] = [0, 0, 0, 0, 0, 0]\n",
    "debt_df['合同负债(元)'] = [0, 0, 0, 0, 949591228.35, 1026782402.35]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "benefit_df = process.read_data('benefit')\n",
    "cash_df = process.read_data('cash')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2017 年之前利息收入和利息费用尚未分开披露\n",
    "\n",
    "财务费用如果是正数，就将其归到利息费用中；相反如果是负数，就是利息收入。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([2016], dtype='int64')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_arr = benefit_df[benefit_df['利息收入(元)'] == 0].index\n",
    "index_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016    78635300.00\n",
       "2017    93430000.00\n",
       "2018   100909000.00\n",
       "2019    84590700.00\n",
       "2020   160282600.00\n",
       "2021   152136800.00\n",
       "Name: 利息收入(元), dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for index in index_arr:\n",
    "    if benefit_df.loc[index, '财务费用(元)'] < 0:\n",
    "        benefit_df.loc[index, '利息收入(元)'] = benefit_df.loc[index, '财务费用(元)'] * -1\n",
    "benefit_df['利息收入(元)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([2016, 2017, 2018], dtype='int64')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_arr = benefit_df[benefit_df['其中：利息费用(元)'] == 0].index\n",
    "index_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016         0.00\n",
       "2017         0.00\n",
       "2018         0.00\n",
       "2019    477400.00\n",
       "2020   6721500.00\n",
       "2021   9638300.00\n",
       "Name: 其中：利息费用(元), dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for index in index_arr:\n",
    "    if benefit_df.loc[index, '财务费用(元)'] > 0:\n",
    "        benefit_df.loc[index, '其中：利息费用(元)'] = benefit_df.loc[index, '财务费用(元)']\n",
    "benefit_df['其中：利息费用(元)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "process.save_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
